/* 
 * 
 */
package com.enjoychange.common.tool;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import com.enjoychange.common.exception.EncryptException;
import com.enjoychange.framework.component.storycase.StoryTracker;

/**
 * MD5加密工具类
 * @author l90003709
 * 
 */
public final class MD5Helper {


	private static Log LOG = LogFactory.getLog(MD5Helper.class);
    /**
     * 加密base16?
     * @param password
     * @return
     */
	public static String encrypt(String password) {
		MessageDigest messageDigest;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.update(password.getBytes("UTF-8"));
			password = byte2hex(messageDigest.digest());
		} catch (NoSuchAlgorithmException e) {
			LOG.error(e.getMessage(), e);
			throw new EncryptException("Encrypt password error",e);			
		} catch (UnsupportedEncodingException e) {
			LOG.error(e.getMessage(), e);
			throw new EncryptException("Encrypt password error",e);	
		}
		return password;
	}
    
	private static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0xFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs;
	}

}
